package com.coinworld.utils;

import com.alibaba.fastjson.JSONObject;
import com.coinworld.constants.CONSTANTS;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 极光im
 * @author admin
 */
public class IMAPI {
    private static RestTemplate restTemplate=new RestTemplate();

    /**
     * //        username : accountId
     * //        password:加密手机号
     * //        nickname:昵称
     * @return
     */
    public static boolean registeredUsers(String userName,String passWord,String nickName){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username",userName);
        jsonObject.put("password",passWord);
        jsonObject.put("nickname",nickName);
        List<JSONObject> objectList=new ArrayList<>();
        objectList.add(jsonObject);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("authorization", CONSTANTS.IM_SDK_KEY);
        HttpEntity<String> formEntity = new HttpEntity<String>(objectList.toString(), headers);
        String s= restTemplate.postForEntity(CONSTANTS.IM_URL,formEntity,String.class).getBody();

        return true;
    }

    /**
     * //        username : accountId
     * //        password:加密手机号
     * //        nickname:昵称
     * @return
     */
    public static String  registeredUserList(List<JSONObject> objectList){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("authorization", CONSTANTS.IM_SDK_KEY);
        HttpEntity<String> formEntity = new HttpEntity<String>(objectList.toString(), headers);
        String s= restTemplate.postForEntity(CONSTANTS.IM_URL,formEntity,String.class).getBody();
        return JSONObject.parseArray(s).get(0).toString();
    }

    /**
     * //        username : accountId
     * //        password:加密手机号
     * //        nickname:昵称
     * @return
     */
    public static String  delList(List<String> objectList){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("authorization", CONSTANTS.IM_SDK_KEY);
        HttpEntity<String> formEntity = new HttpEntity<String>(objectList.toString(), headers);
        restTemplate.delete(CONSTANTS.IM_URL,formEntity,String.class);
        return "";
    }
    /**
     * //        username : accountId
     * //        nickname:昵称
     * @return
     */
    public static boolean editUserNickName(String userName,String nickName){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("nickname",nickName);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("authorization", CONSTANTS.IM_SDK_KEY);
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObject.toString(), headers);
        restTemplate.put( CONSTANTS.IM_URL+"{1}" ,formEntity, userName);
        return true;
    }
}
